package com.ihd.ihardware.view.tzc.discovery.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ItemCommentsBinding;
import com.ihd.ihardware.pojo.CommentsRes;
import com.ihd.ihardware.view.tzc.discovery.model.DiscoveryRepository;
import com.ihd.ihardware.view.tzc.me.view.MyPageActivity;

import java.util.List;

import cn.wowjoy.commonlibrary.adapter.CommonAdapter;

public class CommentsViewModel extends AndroidViewModel {

    private DiscoveryRepository mDiscoveryRepository;

    public CommentsViewModel(@NonNull Application application) {
        super(application);
    }

    private ObservableArrayList<CommentsRes.DataBean.SingCommentsBean> mDT = new ObservableArrayList<>();
    public CommonAdapter mDTAdapter = new CommonAdapter<CommentsRes.DataBean.SingCommentsBean, ItemCommentsBinding>(R.layout.item_comments, mDT, new CommonAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
        }
    }) {
        @Override
        public void convert(ItemCommentsBinding mDataBinding, CommentsRes.DataBean.SingCommentsBean test_concern, int position) {
            if (!TextUtils.isEmpty(test_concern.getAvatar()))
                Glide.with(mDataBinding.concernItemLL.getContext())
                        .load(Uri.parse(test_concern.getAvatar()))
                        .apply(RequestOptions.circleCropTransform())
                        .into(mDataBinding.headIV);
            mDataBinding.headIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), MyPageActivity.class);
                    i.putExtra("userId", test_concern.getUserId());
                    v.getContext().startActivity(i);
                }
            });
        }
    };

    public void getComments(int pageNum, int pageSize, String signId) {
        if (mDiscoveryRepository == null)
            mDiscoveryRepository = new DiscoveryRepository();
        mDiscoveryRepository.getComments(pageNum, pageSize, signId);
    }

    public void setComments(List<CommentsRes.DataBean.SingCommentsBean> list) {
        mDT.clear();
        if (list != null && list.size() > 0)
            mDT.addAll(list);
    }

    public void sendComment(String signId, String commentsText) {
        if (mDiscoveryRepository == null)
            mDiscoveryRepository = new DiscoveryRepository();
        mDiscoveryRepository.sendComment(signId, commentsText);
    }

    public void delTag(String signId) {
        if (mDiscoveryRepository == null)
            mDiscoveryRepository = new DiscoveryRepository();
        mDiscoveryRepository.delTag(signId);
    }

    public void tag(String signId) {
        if (mDiscoveryRepository == null)
            mDiscoveryRepository = new DiscoveryRepository();
        mDiscoveryRepository.tag(signId);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (null != mDiscoveryRepository) {
            mDiscoveryRepository.onDestroy();
            mDiscoveryRepository = null;
        }
    }
}
